package com.ruoyi.project.visit.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.visit.domain.DoctorRegister;
import com.ruoyi.project.visit.service.IDoctorRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 挂号控制器
 */
@RestController
@RequestMapping("/visit/register")
public class DoctorRegisterController extends BaseController {

    @Autowired
    private IDoctorRegisterService doctorRegisterService;

    /**
     * 接诊
     * @param registerId 挂号主键
     * @return
     */
    @PutMapping("/treat/{registerId}")
    public AjaxResult treatPatient(@PathVariable Integer registerId){
        // 创建挂号对象
        DoctorRegister doctorRegister = new DoctorRegister();
        // 设置主键
        doctorRegister.setRegisterId(registerId);
        // 设置更新人，即接诊医生。
        doctorRegister.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(doctorRegisterService.updateTreatPatient(doctorRegister));
    }

    /**
     * 根据状态查询挂号
     * @param registerStatus
     * @return
     */
    @GetMapping("/listByStatus/{registerStatus}")
    public AjaxResult listByStatus(@PathVariable Integer registerStatus){
        return AjaxResult.success(doctorRegisterService.selectListByRegisterStatus(registerStatus));
    }

    /**
     * 条件分页查询挂号
     * @param doctorRegister
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getList(DoctorRegister doctorRegister){
        startPage(); // 分页
        return getDataTable(doctorRegisterService.selectList(doctorRegister));
    }

    /**
     * 新增挂号
     * @param doctorRegister
     * @return
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('visit:register:register')")
    @Log(title = "看病就诊管理", businessType = BusinessType.INSERT)
    public AjaxResult addDoctorRegister(@RequestBody DoctorRegister doctorRegister){
        doctorRegister.setCreateBy(SecurityUtils.getUsername()); // 创建者名字
        return toAjax(doctorRegisterService.insertDoctorRegister(doctorRegister));
    }

}
